//
//  CustomTextFiled.m
//  ArtDog
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CustomTextFiled.h"
#import "Masonry.h"
#import "UIColor+Common.h"

@interface CustomTextFiled ()

@property (nonatomic, strong) UIView *lineView;

@end

@implementation CustomTextFiled

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
        [self viewAddconstraints];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupSubviews];
        [self viewAddconstraints];
        
    }
    return self;
}

- (void)setupSubviews
{
    _leftImageView = [[UIImageView alloc]init];
    [self addSubview:_leftImageView];
    _leftImageView.image = self.leftImage;
    
    _textField = [[UITextField alloc]init];
    [self addSubview:_textField];
    _textField.backgroundColor = [UIColor clearColor];
    _textField.layer.borderWidth = 0;
    _textField.font = [UIFont systemFontOfSize:14];
    
    
    _lineView = [[UIView alloc]init];
    [self addSubview:_lineView];
    _lineView.backgroundColor = [UIColor colorFromHexRGB:@"e0e0e0"];
    
    
    _rightImageView = [[UIImageView alloc]init];
    [self addSubview:_rightImageView];
    
    _rightBtn = [[UIButton alloc]init];
    [self addSubview:_rightBtn];
    _rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    if (_placeholder != nil) {
        _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorFromHexRGB:@"707070"]}];
    }
}

- (void)setLeftImage:(UIImage *)leftImage
{
    _leftImage = leftImage;
    if (_leftImageView != nil) {
        _leftImageView.image = leftImage;
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        [self layoutIfNeeded];
    }
}

- (void)viewAddconstraints
{
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.centerY.equalTo(_textField);
        make.height.equalTo(self).offset(-20);
        make.width.mas_equalTo(0);
       
    }];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_leftImageView.mas_right).offset(10);
        make.top.equalTo(self);
        make.bottom.equalTo(self).offset(-1);

    }];
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textField.mas_right);
        make.height.equalTo(self).offset(-20);
        make.centerY.equalTo(_textField);
        make.width.equalTo(_rightImageView.mas_height);
    }];
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_rightImageView.mas_right);
        make.centerY.equalTo(_textField);
        make.right.equalTo(self).offset(-10);
        make.width.mas_lessThanOrEqualTo(100);
    }];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(_textField.mas_bottom);
        make.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
}

- (void)updateConstraints
{
    if (_leftImageView.image != nil) {
        [_leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.centerY.equalTo(_textField);
            make.height.equalTo(self).offset(-20);
            make.width.equalTo(_leftImageView.mas_height);
        }];
    }
    if (_rightBtn.titleLabel.text.length != 0) {
        [_rightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(_rightBtn.titleLabel.text.length * 14);
        }];
    }
    [super updateConstraints];
}

@end
